package com.sjzxywlkj.cplife.alipay.result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alipay.api.AlipayApiException;

import com.alipay.api.domain.CPCommServices;
import com.alipay.api.domain.CPCommunitySet;
import com.alipay.api.response.AlipayEcoCplifeCommunityBatchqueryResponse;
import com.alipay.api.response.AlipayEcoCplifeCommunityCreateResponse;
import com.alipay.api.response.AlipayEcoCplifeCommunityDetailsQueryResponse;
import com.alipay.api.response.AlipayEcoCplifeCommunityModifyResponse;
import com.sjzxywlkj.cplife.alipay.conn.CommunityConn;
import com.sjzxywlkj.cplife.alipay.request.entity.CommunityBatchquery;
import com.sjzxywlkj.cplife.alipay.request.entity.CommunityCreate;
import com.sjzxywlkj.cplife.alipay.request.entity.CommunityDetailsQuery;
import com.sjzxywlkj.cplife.alipay.request.entity.CommunityModify;
import com.sjzxywlkj.cplife.alipay.response.entity.*;

/**
 * 小区操作返回结果
 */

public class CommunityAlipay {

    //创建物业小区
    public static Map<String, Object> create(CommunityCreate communityCreate, String app_auth_token) throws AlipayApiException {

        AlipayEcoCplifeCommunityCreateResponse response = CommunityConn.create(communityCreate, app_auth_token);


        Map<String, Object> result = new HashMap<String, Object>();
        PublicParameters parameters = new PublicParameters(response.getCode(), response.getMsg(), response.getSubCode(), response.getSubMsg(), null);
        CommunityCreateResult createResult = new CommunityCreateResult(response.getCommunityId(), response.getStatus(), response.getNextAction());
        result.put("parameters", parameters);
        result.put("createResult", createResult);
        return result;

    }

    //变更物业小区信息
    public static Map<String, Object> modify(CommunityModify communityModify, String app_auth_token) throws AlipayApiException {

        AlipayEcoCplifeCommunityModifyResponse response = CommunityConn.modify(communityModify, app_auth_token);

        Map<String, Object> result = new HashMap<String, Object>();

        PublicParameters parameters = new PublicParameters(response.getCode(), response.getMsg(), response.getSubCode(), response.getSubMsg(), null);
        CommunityModifyResult modifyResult = new CommunityModifyResult(response.getStatus(), response.getNextAction());
        result.put("parameters", parameters);
        result.put("modifyResult", modifyResult);
        return result;

    }

    //查询单个物业小区信息
    public static Map<String, Object> query(CommunityDetailsQuery communityDetailsQuery, String app_auth_token) throws AlipayApiException {

        AlipayEcoCplifeCommunityDetailsQueryResponse response = CommunityConn.query(communityDetailsQuery, app_auth_token);

        Map<String, Object> result = new HashMap<String, Object>();
        PublicParameters parameters = new PublicParameters(response.getCode(), response.getMsg(), response.getSubCode(), response.getSubMsg(), null);
        List<Community_services> community_services = new ArrayList<Community_services>();

        if (response.getCommunityServices() != null) {
            for (CPCommServices cServices : response.getCommunityServices()) {
                Community_services services = new Community_services(cServices.getServiceType(), cServices.getCategoryName(), cServices.getExternalInvokeAddress(), cServices.getExternalAddressScanResult(), cServices.getAccountType(), cServices.getAccount(), cServices.getStatus(), cServices.getNextAction(), cServices.getAuditStatus(), cServices.getAuditDesc(), cServices.getServiceExpires(), cServices.getQrCodeImage(), cServices.getQrCodeType(), cServices.getQrCodeExpires(), cServices.getGmtCreated(), cServices.getGmtModified());
                community_services.add(services);
            }
        }
        CommunityDetailsQueryResult detailsQueryResult = new CommunityDetailsQueryResult(response.getCommunityStatus(), response.getNextAction(), response.getAuditStatus(), response.getOutCommunityId(), response.getMerchantPid(), response.getMerchantFirmName(), response.getCommunityName(), response.getCommunityAddress(), response.getDistrictCode(), response.getCityCode(), response.getProvinceCode(), response.getCommunityLocations(), response.getAssociatedPois(), response.getHotline(), response.getIsvPid(), response.getQrCodeImage(), community_services);
        result.put("parameters", parameters);
        result.put("detailsQueryResult", detailsQueryResult);
        return result;

    }

    //批量查询支付宝小区编号
    public static Map<String, Object> batchquery(CommunityBatchquery communityBatchQuery, String app_auth_token) throws AlipayApiException {
        AlipayEcoCplifeCommunityBatchqueryResponse response = CommunityConn.batchquery(communityBatchQuery, app_auth_token);

        Map<String, Object> result = new HashMap<String, Object>();
        PublicParameters parameters = new PublicParameters(response.getCode(), response.getMsg(), response.getSubCode(), response.getSubMsg(), null);
        List<Community_list> community_lists = new ArrayList<Community_list>();

        if (response.getCommunityList() != null) {
            for (CPCommunitySet cSet : response.getCommunityList()) {
                Community_list cList = new Community_list(cSet.getCommunityId(), cSet.getMerchantPid(), cSet.getStatus(), cSet.getOutCommunityId());
                community_lists.add(cList);
            }
        }

        CommunityBatchqueryResult communityBatchqueryResult = new CommunityBatchqueryResult(new Long(response.getCurrentPageNum()).intValue(), new Long(response.getTotalCommunityCount()).intValue(), community_lists);

        result.put("parameters", parameters);
        result.put("communityBatchqueryResult", communityBatchqueryResult);
        return result;


    }
}
